package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import models.Operator;
import models.Oplog;
import services.AuthorityService;
import services.HraDetailService;
import services.SjbbService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import vos.HraDetailView;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.SjbbSearchVo;
import vos.SjbbVo;

public class SjbbController extends BaseController {
	/**
	 * 数据报表页面
	 *
	 * @Title: tjsjbb 
	 * @return: void
	 */
	public static void tjsjbb(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取数据报表
	 *
	 * @Title: gettjsjbb 
	 * @return: void
	 */
	public static void gettjsjbb(int page,int rows, String sort,String order,
			String dealerid,String shopid,String deviceid,String begintime,String endtime,String tjfs){
		if(StringUtils.isEmpty(tjfs)){
			tjfs="dealerid";
		}
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		SjbbSearchVo vo=new SjbbSearchVo(page,rows,order,sort,dealerid,shopid,deviceid,begintime,endtime,tjfs);
		long total=SjbbService.countSjbb(vo);
		ReturnListVo listVo=new ReturnListVo();
		List<SjbbVo> lists=SjbbService.getSjbb(vo);
		listVo.setTotal(total);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 获取合格数据
	 *
	 * @Title: getStandard 
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param id
	 * @return: void
	 */
	public static void getStandard(int page,int rows, String sort,String order,
			String id,String tjfs,String begintime,String endtime){
		if(StringUtils.isEmpty(id)){
			ReturnListVo listVo=new ReturnListVo((long)0,null);
			renderJSON(listVo);
		}
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		SjbbSearchVo vo=new SjbbSearchVo(page,rows,order,sort,id,tjfs,begintime,endtime);
		long total=HraDetailService.countStandard(vo);
		ReturnListVo listVo=new ReturnListVo();
		List<HraDetailView> lists=HraDetailService.getStandard(vo);
		listVo.setTotal(total);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 获取不合格数据
	 *
	 * @Title: getCount 
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param id
	 * @return: void
	 */
	public static void getCount(int page,int rows, String sort,String order,
			String id,String tjfs,String begintime,String endtime){
		if(StringUtils.isEmpty(id)){
			ReturnListVo listVo=new ReturnListVo((long)0,null);
			renderJSON(listVo);
		}
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		SjbbSearchVo vo=new SjbbSearchVo(page,rows,order,sort,id,tjfs,begintime,endtime);
		long total=HraDetailService.countAll(vo);
		ReturnListVo listVo=new ReturnListVo();
		List<HraDetailView> lists=HraDetailService.getAll(vo);
		listVo.setTotal(total);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 导出为excel
	 *
	 * @Title: exportXls 
	 * @param dealerid 经销商id
	 * @param shopid 门店id
	 * @param deviceid 设备id
	 * @param begintime 开始时间
	 * @param endtime 结束时间
	 * @param tjfs 统计方式
	 * @return: void
	 */
	public static void exportXls(String dealerid,String shopid,String deviceid,String begintime,String endtime,String tjfs){
		if(!checkAuthority("SJBB_EXPORT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		SjbbSearchVo vo=new SjbbSearchVo(dealerid,shopid,deviceid,begintime,endtime,tjfs);
		List<SjbbVo> lists=SjbbService.exportXls(vo);
		long y=System.currentTimeMillis();
		String fileName = "统计报表"+y+".csv";
        response.contentType = "application/x-download";
        Operator oper=getLoginUser();
        Oplog oplog=new Oplog(oper.operatorid,"导出数据报表数据","数据报表");
		oplog.save(oplog);
        renderBinary(ExcelUtil.buildExportFile(FileConfig.sjbbXlsTemplate, lists), fileName);
	}
}
